//@ts-nocheck
var goooods = {
  umm: [],
  emm: []
};

function Cates() {
  this.name = ""; //类别名称
  this.attris = []; //类别值
}

//点击按钮提交事件
document.getElementById("submit").addEventListener("click", addNewItem);

function addNewItem() {
  let code = document.getElementById("lq_num").value;
  let name = document.getElementById("lq_name").value;
  let introduce = document.getElementById("lq_introduce").value;
  let symbol = "￥";
  let currentPrice = document.getElementById("lq_lP").value;
  let originalPrice = document.getElementById("lq_nP").value;
  let salenum = document.getElementById("lq_sP").value;
  let deadline = countDown();
  let ServiceInfo = "货到付款";

  let data = {
    code: code,
    name: name,
    introduce: introduce,
    symbol: symbol,
    currentPrice: currentPrice,
    originalPrice: originalPrice,
    salenum: salenum,
    deadline: deadline,
    ServiceInfo: ServiceInfo
  };
  goooods.emm = changeStringEmm(goooods.emm);

  console.log(goooods.emm);

  //图片上传
  let fd = new FormData();
  fd.append("file", document.getElementById("file1").files[0]);
  $.ajax({
    type: "POST",
    data: fd,
    url: "php/uploadimg.php",
    contentType: false,
    processData: false,
    cache: false,
    dataType: "json"
  })
    .done(function(res) { alert(res);})
    .fail(function() {});

  let fdd = new FormData();
  fdd.append("file", document.getElementById("file2").files[0]);
  $.ajax({
    type: "POST",
    data: fdd,
    url: "php/uploadimg.php",
    contentType: false,
    processData: false,
    cache: false,
    dataType: "json"
  })
    .done(function(res) {})
    .fail(function() {});

  $.ajax({
    type: "GET",
    url: "php/styleinsert.php",
    data: goooods.emm,
    dataType: "json",
    success: (function() {})()
  });

  $.ajax({
    type: "GET",
    url: "php/goodsinsert.php",
    data: data,
    dataType: "json",
    success: (function() {
      alert("成功!")
    })()
  });
}

//图片上传
// $("#subMainPic").bind("click", function () {
//   var fd = new FormData();
//   fd.append("file", document.getElementById("file1").files[0]);
//   $.ajax({
//     type: "POST",
//     data: fd,
//     url: "php/uploadimg.php",
//     contentType: false,
//     processData: false,
//     cache: false,
//     dataType: "json",
//   }).done(function (res) {
//     alert(res);
//   }).fail(function (res) {
//     alert(res);
//   });
// });

//输入框添加检查事件
$("#lq_num").blur(function() {
  let code = document.getElementById("lq_num").value;
  if (!checkABC123(code) && code !== "") {
    document.getElementById("lq_num_tip").className = "";
    document.getElementById("lq_num_tip").className =
      "glyphicon glyphicon-warning-sign form-control-feedback";
    $("#lq_num_tip").html("   只能输入数字和字母");
  } else if (code === "") {
    document.getElementById("lq_num_tip").className = "";
    document.getElementById("lq_num_tip").className =
      "glyphicon glyphicon-remove form-control-feedback";
    $("#lq_num_tip").html("   不能为空");
  } else {
    document.getElementById("lq_num_tip").className = "";
    document.getElementById("lq_num_tip").className =
      "glyphicon glyphicon-ok form-control-feedback";
    $("#lq_num_tip").html("");
  }
});
$("#lq_name").blur(function() {
  let name = document.getElementById("lq_name").value;
  if (name === "") {
    document.getElementById("lq_name_tip").className = "";
    document.getElementById("lq_name_tip").className =
      "glyphicon glyphicon-remove form-control-feedback";
    $("#lq_name_tip").html("   不能为空");
  } else {
    document.getElementById("lq_name_tip").className = "";
    document.getElementById("lq_name_tip").className =
      "glyphicon glyphicon-ok form-control-feedback";
    $("#lq_name_tip").html("");
  }
});
$("#lq_lP,lq_nP").blur(function() {
  let currentPrice = document.getElementById("lq_lP").value;
  let originalPrice = document.getElementById("lq_nP").value;
  if (
    checkNumber(currentPrice) &&
    checkNumber(originalPrice) &&
    originalPrice !== "" &&
    currentPrice !== "" &&
    currentPrice <= originalPrice
  ) {
    document.getElementById("lq_P_tip").className = "";
    document.getElementById("lq_P_tip").className =
      "glyphicon glyphicon-ok form-control-feedback";
    $("#lq_P_tip").html("");
  } else {
    document.getElementById("lq_P_tip").className = "";
    document.getElementById("lq_P_tip").className =
      "glyphicon glyphicon-warning-sign form-control-feedback";
    $("#lq_P_tip").html("   请输入准确数字(现价低于原价)");
  }
});
$("#lq_sN").blur(function() {
  let salenum = document.getElementById("lq_sN").value;
  if (salenum === "") {
    document.getElementById("lq_sN_tip").className = "";
    document.getElementById("lq_sN_tip").className =
      "glyphicon glyphicon-remove form-control-feedback";
    $("#lq_sN_tip").html("   不能为空");
  } else if (!checkIntNumber(salenum)) {
    document.getElementById("lq_sN_tip").className = "";
    document.getElementById("lq_sN_tip").className =
      "glyphicon glyphicon-remove form-control-feedback";
    $("#lq_sN_tip").html("   请输入整数");
  } else {
    document.getElementById("lq_sN_tip").className = "";
    document.getElementById("lq_sN_tip").className =
      "glyphicon glyphicon-ok form-control-feedback";
    $("#lq_sN_tip").html("");
  }
});
$("#lq_sP").blur(function() {
  let salenum = document.getElementById("lq_sP").value;
  if (salenum === "") {
    document.getElementById("lq_sP_tip").className = "";
    document.getElementById("lq_sP_tip").className =
      "glyphicon glyphicon-remove form-control-feedback";
    $("#lq_sP_tip").html("   不能为空");
  } else if (!checkIntNumber(salenum)) {
    document.getElementById("lq_sP_tip").className = "";
    document.getElementById("lq_sP_tip").className =
      "glyphicon glyphicon-remove form-control-feedback";
    $("#lq_sP_tip").html("   请输入整数");
  } else {
    document.getElementById("lq_sP_tip").className = "";
    document.getElementById("lq_sP_tip").className =
      "glyphicon glyphicon-ok form-control-feedback";
    $("#lq_sP_tip").html("");
  }
});

//输入框事件检查功能
//检查只能输入数字和字母
function checkABC123(val) {
  let reg = /^[0-9a-zA-Z]+$/;
  return reg.test(val);
}
//只能输入整数或者小数
function checkNumber(val) {
  let reg = /^[0-9]+([.]{1}[0-9]+){0,1}$/;
  return reg.test(val);
}
//只能输入整数
function checkIntNumber(val) {
  let reg = /^\+?[0-9][0-9]*$/;
  return reg.test(val);
}

//添加种类处理方式
document.getElementById("lq_umm_button").addEventListener("click", () => {
  let umm = goooods.umm;
  addumm(umm);
  DomUmm(umm);
  DomAttribute(umm);
  let tableList = document.getElementById("lq_emm_List");
  tableList.innerHTML = "";
  goooods.emm = [];
});
//数据上添加种类
function addumm(umm) {
  let newCate = new Cates();

  let name = document.getElementById("lq_umm_cate").value;
  let attri = document.getElementById("lq_umm_attri").value;
  let attris = [];
  attris = attri.split("#");

  newCate = {
    name,
    attris
  };

  umm.push(newCate);
}
//点击在Dom上渲染种类
function DomUmm(ummList) {
  let tableList = document.getElementById("lq_cateList");

  tableList.innerHTML = `<tr>
                            <th>类别</th>
                            <th>参数</th>
                            <th></th>
                        </tr>`;

  ummList.forEach((element, index) => {
    let tr = document.createElement("tr");
    tr.innerHTML = `<td>${element.name}</td> 
                    <td>${element.attris.join("    ")}</td>
                    <td><button class="btn btn-primary deButton">删除</button></td>`;
    tableList.getElementsByTagName("tbody")[0].appendChild(tr);
    tr.getElementsByTagName("button")[0].addEventListener("click", () => {
      goooods.umm.splice(index, 1);
      DomUmm(ummList);
      DomAttribute(ummList);
      let tableList = document.getElementById("lq_emm_List");
      tableList.innerHTML = "";
      goooods.emm = [];
    });
  });
}

//添加款式处理方式
document.getElementById("lq_emm_button").addEventListener("click", addDomClick);

function addDomClick() {
  let umm = goooods.umm;
  let emm = goooods.emm;
  getOpetionValue(umm, emm);
  DomEmm(umm, emm);
  changeStringEmm(emm);
}

//把类别渲染到Dom上
function DomAttribute(umm) {
  let selectList = document.getElementById("lq_emm");
  selectList.innerHTML = `<button id="lq_emm_button" onclick="addDomClick()"class="btn btn-primary addAttriButton">确认添加</button>
  <span class="inlineSpan"> 添加商品款式:</span>
  <label for="">价格:<input type="text" name="" id="lq_emm_price" class="form-control inputNum">元</label>`;

  umm.forEach(element => {
    let label = document.createElement("label");
    label.innerHTML = `${element.name}:`;
    let select = document.createElement("select");
    element.attris.forEach(element => {
      let option = document.createElement("option");
      option.value = `${element}`;
      option.innerHTML = `${element}`;
      select.appendChild(option);
    });
    label.appendChild(select);
    selectList.appendChild(label);
  });
}
//数据上获取各个option的value值放到emm中
function getOpetionValue(umm, emm) {
  let newAttri = {};
  let selectValue = document
    .getElementById("lq_emm")
    .getElementsByTagName("select");
  for (let i = 0; i < selectValue.length; i++) {
    let name = umm[i].name;
    let Avalue = selectValue[i].value;
    newAttri[name] = Avalue;
  }
  newAttri.price = document.getElementById("lq_emm_price").value;
  emm.push(newAttri);
}
//点击在Dom上渲染参数属性 table
function DomEmm(umm, emm) {
  let tableList = document.getElementById("lq_emm_List");
  tableList.innerHTML = "";

  let tr = document.createElement("tr");
  umm.forEach(element => {
    let td = document.createElement("th");
    td.innerHTML = `${element.name}`;
    tr.appendChild(td);
  });
  let td = document.createElement("th");
  td.innerHTML = `价格`;
  tr.appendChild(td);

  let thead = document.createElement("thead");
  thead.appendChild(tr);
  tableList.appendChild(thead);

  let tbody = document.createElement("tbody");
  emm.forEach(element => {
    let tr = document.createElement("tr");
    let index;
    for (index in element) {
      let td = document.createElement("td");
      td.innerHTML = `${element[index]}`;
      tr.appendChild(td);
    }

    tbody.appendChild(tr);
  });
  tableList.appendChild(tbody);
}

//倒计时转换,时间控制
$(".form_datetime").datetimepicker({
  weekStart: 1,
  todayBtn: 1,
  autoclose: 1,
  todayHighlight: 1,
  startView: 2,
  forceParse: 0,
  showMeridian: 1
});

$("#lq_time_date").bind("input propertychange", function() {
  let aa = countDown();
  if (judgeDate(aa) == 1) {
    document.getElementById("lq_time_tip").className = "";
    document.getElementById("lq_time_tip").className =
      "glyphicon glyphicon-ok form-control-feedback";
    $("#lq_time_tip").html("");
  } else {
    document.getElementById("lq_time_tip").className = "";
    document.getElementById("lq_time_tip").className =
      "glyphicon glyphicon-warning-sign form-control-feedback";
    $("#lq_time_tip").html("   请选择有效时间");
  }
});

function countDown() {
  let date = document.getElementById("lq_time_date").value;
  let dateSecond = Date.parse(date);
  return Date.parse(dateSecond.toString());
}

function judgeDate(goal) {
  let now = new Date();
  let nowDate = Date.parse(now.toString());
  if (goal - nowDate <= 0) {
    return -1;
  } else if (goal - nowDate >= 3599999) {
    return 0;
  } else {
    return 1;
  }
}

//改变emm里面的字符串格式
function changeStringEmm(emm) {
  let umm = [];
  emm.forEach(element => {
    let name = [];
    let price = parseInt(element.price);
    let attri = "";
    for (attri in element) {
      if (attri !== "price") {
        name.push(element[attri].toString());
      }
    }
    element = {};
    element = {
      style: name.join("-"),
      price: price
    };
    umm.push(element);
  });

  return umm;
}

function getKeys() {
  let k = document.getElementById("lq_keys").value;
  let keys = [];
  keys = k.split("#");
}

loadScript("js/item/wangEditor.js", () => {
  var E = window.wangEditor;
  var editor2 = new E("#editor");
  editor2.customConfig.uploadImgShowBase64 = true;
  editor2.customConfig.showLinkImg = false;
  editor2.create();
});
